package cn.com.dhcc.turbo.service.exch.expired;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import cn.com.dhcc.app.core.AppEnv;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.Status;
import cn.com.dhcc.app.pub.core.consts.AppIocBean;

/**
 *时间过期策略 
 *在某一个时间段内，消息还没发送出去，失败
 */
@Component(AppIocBean.TIMEOUT_EXPIRED_STRATEGY_BEAN)
public class TimeoutExpiredStrategy implements ExpiredStrategy {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getDetachLogger(LOGTYPE.TURBO);
	private static DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static final Integer EXPIRED_TIME = AppEnv.ParaGetTool.getPara(Integer.class, "expired.strategy.timeout", 3);

	@Override
	public Status isExpired(Object... obj) {
		Status status = Status.getInstance();
		String msg = "";
		try {
			Date now = new Date();
			Date created = (Date) obj[0];
			Date expired = DateUtils.addMinutes(created, EXPIRED_TIME);
			if (expired.before(now)) {
				msg = "数据发送失败：消息已过期。消息接收时间[" + df1.format(created) + "]，消息过期时间[" + df1.format(expired) + "]，当前时间["+ df1.format(now) + "]。";
				status.setSuccess(true);
				status.setMsg(msg);
			}
		} catch (Exception e) {
			logger.error("TimeoutExpiredStrategy isExpired Exception", e);
		}
		return status;
	}

}
